//
//  CYStatusPictureView.swift
//  WeiBo
//
//  Created by Mr_J on 16/3/12.
//  Copyright © 2016年 mac. All rights reserved.
//


import UIKit
import SDWebImage

let SCREENW = UIScreen.mainScreen().bounds.width
let SCREENH = UIScreen.mainScreen().bounds.height

let itemMargin: CGFloat = 5

private let itemWH = CGFloat(Int(SCREENW - 2 * CYStatusCellMargin - 2 * itemMargin)/3)

class CYStatusPictureView: UICollectionView {
    
    private let CYStatusPictureViewCellId = "CYStatusPicViewID"

   
    var picUrls: [CYStatusPictureInfo]? {
        
        didSet{
            label.text = "\(picUrls?.count ?? 0)"
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(self.sizeWithCount(picUrls?.count ?? 0))
            }
            
            
            self.reloadData()
        }
        
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func sizeWithCount(count: Int) ->CGSize {
        
        if count == 1 {
            
            if let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picUrls?.first?.thumbnail_pic) {
                
                var size = image.size
                
                if size.width < 80 {
                    
                    size.height = 80 / size.width * size.height
                    size.width = 80
                }
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                layout.itemSize = size
                
                return size
            }
 
        }
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        

        
        // 1. 计算出每一张图片多大
        
        // 2. 计算出有多少列和多少行
        let col = count == 4 ? 2 : (count > 3 ? 3 : count)
        let row = (count - 1) / 3 + 1
        
        // 3. 根据列数和行数,计算整个配图视图的大小
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: width, height: height)
    }
    
    
    
    
    private func setupUI() {
        backgroundColor = UIColor.yellowColor()
        
        self.dataSource = self
        
        registerClass(CYStatusPictureViewCell.self, forCellWithReuseIdentifier: CYStatusPictureViewCellId)
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
//        addSubview(label)
//        
//        label.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self)
//        }
        
    
    }
    
    private lazy var label: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 35)
    
    
    
}

extension CYStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CYStatusPictureViewCellId, forIndexPath: indexPath) as! CYStatusPictureViewCell
        
        cell.backgroundColor = UIColor.darkGrayColor()
        
        cell.pictureInfo = picUrls![indexPath.item]
        
        return cell
    }
}
class CYStatusPictureViewCell: UICollectionViewCell {
    
    var pictureInfo: CYStatusPictureInfo? {
        didSet{
            guard let urlString = pictureInfo?.thumbnail_pic else {
                return
            }
            imageView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "timeline_image_placeholder"))
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
     
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
       contentView.addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
    }
    
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }()
    
}
